package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseFleetCustomerService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseFleetCustomer;
import com.cloudlead.fleetAction.service.FleetCustomerSer;
import com.cloudlead.fleetService.vo.BaseFleetCustomerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
/**
 * Created by Will on 2015/12/16.
 */
public class FleetCustomerSerImpl implements FleetCustomerSer {
    @Autowired
    BaseFleetCustomerService baseFleetCustomerService;
    @Override
    public String addFleetCustomer(BaseFleetCustomerVo baseFleetCustomerVo) {
        ifExist(baseFleetCustomerVo);
        BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
        BaseCompany baseCompany = new BaseCompany();
        if(null != baseFleetCustomerVo.getBaseCompany() && !"".equals(baseFleetCustomerVo.getBaseCompany())){
            baseCompany.setId(baseFleetCustomerVo.getBaseCompany());
        }
        BeanUtils.copyProperties(baseFleetCustomerVo,baseFleetCustomer);
        baseFleetCustomer.setBaseCompany(baseCompany);
        baseFleetCustomer.setFlag(baseFleetCustomerVo.getFlag1());
        baseFleetCustomerService.create(baseFleetCustomer);

        return baseFleetCustomer.getId();
    }
    @Override
    public String delFleetCustomer(String fleetCustomerId) {
        BaseFleetCustomer baseFleetCustomer = baseFleetCustomerService.view(fleetCustomerId);
        baseFleetCustomer.setFlag("1");
        baseFleetCustomerService.update(baseFleetCustomer);
        return "OK";
    }
    @Override
    public String editFleetCustomer(BaseFleetCustomerVo baseFleetCustomerVo) {
        ifExist(baseFleetCustomerVo);
        BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
        BaseCompany baseCompany = new BaseCompany();
        if(null != baseFleetCustomerVo.getBaseCompany() && !"".equals(baseFleetCustomerVo.getBaseCompany())){
            baseCompany.setId(baseFleetCustomerVo.getBaseCompany());
        }
        BeanUtils.copyProperties(baseFleetCustomerVo,baseFleetCustomer);
        baseFleetCustomer.setFlag(baseFleetCustomerVo.getFlag1());
        baseFleetCustomer.setBaseCompany(baseCompany);
        baseFleetCustomerService.update(baseFleetCustomer);
        return baseFleetCustomer.getId();
    }
    @Override
    public Page findAll(BaseFleetCustomerVo baseFleetCustomerVo,int rows,int page) {
        PageRequest pageRequest = new PageRequest();
        List<Term> list = new ArrayList<Term>();
        if(null != baseFleetCustomerVo.getFlag1() && !"".equals(baseFleetCustomerVo.getFlag1())){
            list.add(new Term("flag",baseFleetCustomerVo.getFlag1()));
        }
        if(null != baseFleetCustomerVo.getId() && !"".equals(baseFleetCustomerVo.getId())){
            list.add(new Term("id",baseFleetCustomerVo.getId()));
        }
        if(null != baseFleetCustomerVo.getProvince() && !"".equals(baseFleetCustomerVo.getProvince())){
            list.add(new Term("province",baseFleetCustomerVo.getProvince()));
        }
        if(null != baseFleetCustomerVo.getCity() && !"".equals(baseFleetCustomerVo.getCity())){
            list.add(new Term("city",baseFleetCustomerVo.getCity()));
        }
        if(null != baseFleetCustomerVo.getCompanyShortName() && !"".equals(baseFleetCustomerVo.getCompanyShortName())){
            list.add(new Term("companyShortName",baseFleetCustomerVo.getCompanyShortName()));
        }
        if(null != baseFleetCustomerVo.getState() && !"".equals(baseFleetCustomerVo.getState())){
            list.add(new Term("state",baseFleetCustomerVo.getState()));
        }
        if(null != baseFleetCustomerVo.getBaseCompany() && !"".equals(baseFleetCustomerVo.getBaseCompany())){
            list.add(new Term("baseCompany.id", baseFleetCustomerVo.getBaseCompany()));
        }
        Filter filter  = new Filter(new Criteria(list));
        pageRequest.setFilter(filter);
        pageRequest.setSize(rows);
        pageRequest.setPage(page);
        return  baseFleetCustomerService.findAll(pageRequest);
    }

    @Override
    public void hfBaseDriver(String id) {
        BaseFleetCustomer bd=baseFleetCustomerService.view(id);
        bd.setFlag("0");
        baseFleetCustomerService.update(bd);
    }

    public String ifExist(BaseFleetCustomerVo baseFleetCustomerVo){
        BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
        baseFleetCustomer.setCompanyName(baseFleetCustomerVo.getCompanyName());
        BaseCompany baseCompany=new BaseCompany();
        baseCompany.setId(baseFleetCustomerVo.getBaseCompany());
        baseFleetCustomer.setBaseCompany(baseCompany);
        baseFleetCustomer  = baseFleetCustomerService.findOne(baseFleetCustomer);
        if(null != baseFleetCustomerVo.getId() && !"".equals(baseFleetCustomerVo.getId())){
            if(baseFleetCustomer != null){
                if(!baseFleetCustomerService.findOne(baseFleetCustomer).getId().equals(baseFleetCustomerVo.getId())){
                    throw new RestClientException("客户："+baseFleetCustomerVo.getCompanyName()+"已存在");
                }
            }
        }else {
            if(baseFleetCustomer != null){
                throw new RestClientException("客户："+baseFleetCustomerVo.getCompanyName()+"已存在");
            }
        }
        return null;
    }
}
